//对 uni.request()请求封装


function request(params) {
    //转圈圈启动!
    // uni.showLoading({
    //     title: '拼命加载中'
    // });
    //发送请求
    return new Promise((resolve, reject) => {
        uni.request({
            ...params, //解构参数(url,methods,data)
            header: {
                'token': uni.getStorageSync('token'),
            },
            success(res) {
                // console.log(res, "封装")
                    //返回成功获取的数据
                switch (res.statusCode) {
                    case 404:
                        uni.showToast({
                            title: '服务器无响应',
                            duration: 2000,
                            icon: 'none'
                        });
                        break;
                    case 400:
                        uni.showToast({
                            title: '参数有误',
                            duration: 2000,
                            icon: 'none'
                        });
                        break;
					case 500:
					    uni.showToast({
					        title: '服务器异常',
					        duration: 2000,	
					        icon: 'none'
					    });
					    break;
                    default:
                        resolve(res.data);
                        break
                }
            },
            fail(err) {
                reject(err)
				uni.showToast({
				    title: '服务器访问失败',
				    duration: 2000,	
				    icon: 'none'
				});
                // console.log(err, 'request封装')
            },
            complete() {
                //转圈圈停止
                // uni.hideLoading()
            }
        })
    })
}

export default request